package com.freecloud.spring.formework.webmvc.servlet;

import java.io.File;
import java.util.Locale;

/**
 * @Author: maomao
 * @Date: 2019-12-16 09:12
 */
public class FCViewResolver {

    private final String DEFAULT_TEMPLATE_SUFFX = ".html";

    private File templateRootDir;

    private String viewName;

    public FCViewResolver(String templateRoot) {
        String templateRootPath = this.getClass().getClassLoader().getResource(templateRoot).getFile();
        this.templateRootDir = new File(templateRootPath);
    }

    public FCView resolveViewName(String viewName, Locale locale) {
        if(viewName == null || "".equals(viewName)){
            return null;
        }
        viewName = viewName.endsWith(DEFAULT_TEMPLATE_SUFFX) ? viewName : viewName + DEFAULT_TEMPLATE_SUFFX;

        File templateFile = new File((templateRootDir.getPath() + "/" + viewName).replaceAll("/+","/"));

        return new FCView(templateFile);
    }


    public String getViewName() {
        return viewName;
    }
}
